<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <router-link :to="path">
        <el-button class="filter-item" type="primary" icon="el-icon-edit">新增</el-button>
      </router-link>
      <el-table :data="list" v-loading="listLoading" empty-text="目前无权限组" element-loading-text="给我一点时间" border fit highlight-current-row
        style="width: 100%" max-height="580">
        <el-table-column width="150px" align="center" label="编号">
          <template slot-scope="scope">
            <span>{{scope.row.id}}</span>
          </template>
        </el-table-column>
        <el-table-column width="220px" align="center" label="名称">
          <template slot-scope="scope">
            <span>{{scope.row.group_name}}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="220px" align="center" label="描述">
          <template slot-scope="scope">
            <span>{{scope.row.group_des}}</span>
          </template>
        </el-table-column>
        <el-table-column width="220px" align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="view" size="mini" @click="handleView(scope.row)">查看</el-button>
            <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[5,10,20,30, 50]" :page-size="listQuery['per-page']" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { fetchGroupList, deleteUserGroup } from '@/api/user'
export default {
  data() {
    return {
      path: '/manager/auth-add',
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        'per-page': 10
      },
      temp: {

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchGroupList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data._meta.totalCount
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery['per-page'] = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    // 查看权限信息详情
    handleView(row) {
      this.$router.push({ path: '/manager/auth-view/' + row.id })
    },
    // 修改权限信息详情
    handleUpdate(row) {
      this.$router.push({ path: '/manager/auth-update/' + row.id })
    },
    // 删除
    handleDelete(row) {
      var obj = this
      this.$confirm('请确保权限组中无成员，如有成员则无法删除，确认要删除此组吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserGroup({ gid: row.id }).then(response => {
          if (response.data.status === 2) {
            this.$message({
              type: 'success',
              message: '删除成功!',
              onClose: function() {
                obj.getList()
              }
            })
          } else {
            this.$message({
              type: 'error',
              message: '删除失败，请先删除权限组中所有成员!'
            })
          }
        })
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消操作'
        // })
      })
    }
  }
}
</script>
